import chart from '@/utils/chart.js'
import { toYYYYMMDD, toHHMM, toMMDD, formatTimeStamp } from '@/utils/timeUtil'
import { parseNumFloat } from '@/utils/tool'
import { keyToZh, valToZh } from './codeMaps.js'
import { ONE_DAY_MS } from './consts.js'
export const chartTempFridge = (chartData, time_start, time_end, isOneDay, showModeTip,deviceDetail)=>{
    let series = []
    const key='tem'
    const isArea=true
    let style = chart.styleConfig('#70C3EF', 'solid', isArea)
    let o = {
        data: [],
        name:  keyToZh(key),
        ...chart.ECHART_LINE_CONFIG_BIG_DATA,
        ...style
    }
    series.push(o)
    for (let data of chartData) {
      o.data.push([data.upload, data.data[key]])
    }
    // if (chartData.length === 0) {
    //     for (let i = 0; i <= 24; i++) {
    //       if (i === 24) {
    //         o.data.push([new Date(`${toYYYYMMDD(time_start)} 23:59:59`).getTime(), 0])
    //       } else {
    //         o.data.push([new Date(`${toYYYYMMDD(time_start)} ${i}:00`).getTime(), 0])
    //       }
    //     }
    // } else {
        
    // }

    let xInterval = 1
    if (time_end - time_start >= 11 * 24 * 3600 * 1000) {
      xInterval = 4
    }
  
    let xAxisConfig = null
    // 选择 一天 的x轴config
  if (isOneDay) {
    xAxisConfig = {
      splitLine: {
        show: false
      },
      type: 'time',
      maxInterval: 1 * 3600 * 1000,
      min: function (value) {
        let todayStart = new Date(
          new Date(value.min).toLocaleDateString()
        ).getTime()
        return todayStart
      },
      max: function (value) {
        let todayStart = new Date(
          new Date(value.min).toLocaleDateString()
        ).getTime()
        let todayEnd = todayStart + ONE_DAY_MS
        return todayEnd
      },
      axisLabel: {
        show: true,
        color: '#c3c6cd',
        rotate: 30,
        formatter: (value, index) => {
          let t = toHHMM(new Date(value).getTime())
          return `${t}`
        }
      }
      // data: xData
    }
  } else {
    // 选择 多天 的x轴config
    xAxisConfig = {
      splitLine: {
        show: false
      },
      type: 'time',
      boundaryGap: ['10%', '10%'],
      maxInterval: 5 * 3600 * 24 * 1000,
      min: function (value) {
        let todayStart = new Date(
          new Date(value.min).toLocaleDateString()
        ).getTime()
        return todayStart
      },
      max: new Date().getTime(),
      axisLabel: {
        show: true,
        color: '#c3c6cd',
        rotate: 0,
        formatter: (value, index) => {
          let d = toMMDD(new Date(value).getTime())
          let t = toHHMM(new Date(value).getTime())
          return `${d} ${t}`
        }
      }
      // data: xData
    }
  }

  let option = {
        // legend: {
        //   data: ['设定温度', '房间温度'],
        //   selected: legendSelected,
        //   ...chart.DETAIL_LEGEND
        // },
        grid: {
          left: '3%',
          right: '3%',
          bottom: '3%',
          containLabel: true
        },
        tooltip: {
        ...chart.TOOLTIP,
        formatter: (params, ticket) => {
            let s = ''
            s = s + formatTimeStamp(params[0].axisValue) + `</br>`

            for (let item of params) {
            let unit = ''
            if (item.seriesName.indexOf('温度') >= 0) {
                unit = '°C'
                s += `<span style="color:${item.color}">
                    <span class="tooltip-m">${item.seriesName}: </span>${item.value[1]}${unit}</span></br>`
            } else if (showModeTip) {
                s += `<span style="color:${item.color}">
                    <span class="tooltip-m">${item.seriesName}: </span>${valToZh(item.value[1])}</span></br>`
            }
            }
            return s
        }
        },
        dataZoom: [
        {
            type: 'inside',
            throttle: 50
        }
        ],
        xAxis: {
          type: 'time',
          ...xAxisConfig,
          axisLine: {
            show: true,
            lineStyle: {
              color: '#6E798B'
            }
          },
          axisTick:{
            show:false
          }
        },
        yAxis: {
        name: '°C       ',
        type: 'value',
        ...chart.AXIS_STYLE
        },
        series: series
    }
    return option
}